﻿	function pagenav(curpage) {

		var pagenav = '<li class="mark">'+curpage+'</li>' ;
		var i = 1 ;
		var tmp = 3 ;
		while (i <= tmp) {
			if (curpage-i > 0 ) {
				pagenav = " <li><a href='javascript://' onclick='clickPageNav("+(curpage-i)+");'>" + (curpage-i) + "</a></li> " + pagenav ;
			}
			if (curpage+i <= TotalPage) {
				pagenav = pagenav + " <li><a href='javascript://' onclick='clickPageNav("+(curpage+i)+");'>" + (curpage+i) + "</a></li>" ;
			}
			i++ ;
		}
		if (curpage > 1 ) {
			if ((curpage-10)>0){
				pagenav = "<li><a href='javascript://' onclick='clickPageNav(1)'>第1页</a></li> <li><a href='javascript://' onclick='clickPageNav("+(curpage-10)+")'>上10页</a></li> " + pagenav ;
			} else {
				pagenav = "<li><a href='javascript://' onclick='clickPageNav(1)'>第1页</a></li> <li><a href='javascript://' onclick='clickPageNav("+ (curpage-1) +")'>上一页</a></li> " + pagenav ;
			}
		}
		if (curpage < TotalPage) {
			if ((curpage+10)<TotalPage) {
				pagenav = pagenav + " <li><a href='javascript://' onclick='clickPageNav("+(curpage+10)+")'>下10页</a></li> <li><a href='javascript://' onclick='clickPageNav("+ TotalPage +")'>第"+TotalPage+"页</a></li>";
			} else {
				pagenav = pagenav + " <li><a href='javascript://' onclick='clickPageNav("+ (curpage+1) +")'>下一页</a></li> <li><a href='javascript://' onclick='clickPageNav("+ TotalPage +")'>第"+TotalPage+"页</a></li>" ;
			}
		}
			//pagenav = pagenav + " <li><a href='javascript://' onclick='clickPageNav("+ (curpage+1) +")'>下一页<a></li> <li><a href='javascript://' onclick='clickPageNav("+ TotalPage +")'>第"+TotalPage+"页</a></li>" ;
		pagenav = '<ul>'+pagenav+'</ul>' ;
		//alert(pagenav);
		$('pagenav_top').innerHTML = pagenav ;
		$('pagenav_bottom').innerHTML = pagenav ;
		//alert(pagenav);
	}

	function clickPageNav(p) {
		//var SliderPosition = (1/TotalPage)*(p-1)+(1/(2*TotalPage)) ;
		var SliderPosition = (p-1)*(1/(TotalPage-1)) ;
		slider.setValue(SliderPosition) ;
		//changePage(p);
		//alert(SliderPosition);
	}

	function changePage(p) {
		//alert("Send Ajax Request!") ;
		getComment(p) ;
	}

	function onChange(v){
		var page_num = Math.ceil(v/(1/TotalPage))?Math.ceil(v/(1/TotalPage)):1 ;
		//$('debug1').innerHTML='结束: '+page_num ;
		//alert(CurPage);
		if (page_num != CurPage) {
			pagenav(page_num);
			changePage(page_num);
			CurPage = page_num ;
		}
		$('pagenav_show').style.display= 'none' ;
		
	}

	function onSlide(v){
		var page_num = Math.ceil(v/(1/TotalPage))?Math.ceil(v/(1/TotalPage)):1 ;
		//$('debug1').innerHTML='滑动: '+page_num ;
		$('pagenav_show').style.display = "block" ;
		$('pagenav_show').innerHTML = page_num ;
		//alert("滑动了") ;
	}

	function getComment(cur_page) {
		var random_str = '' ;
		if (NewPost){
			random_str = '&rd='+Math.floor(Math.random()*9999999)+1 ;
		}
		//alert('comment.php?action=get_comment&total_page='+TotalPage+'&cur_page='+cur_page+'&channel='+Channel+'&id='+Id+random_str) ;
		var CommentAjax = new Ajax.Request(
					'comment.php?action=get_comment&total_page='+TotalPage+'&cur_page='+cur_page+'&url='+Url+random_str, // 请求的URL
					{
						method: 'get', 
						onComplete: showComment
					}
				);
	}

	function getIframeComment() {
		var random_str = '' ;
		if (NewPost){
			random_str = '&rd='+Math.floor(Math.random()*9999999)+1 ;
		}
		//alert('comment.php?action=get_iframe_comment&channel='+Channel+'&id='+Id) ;
		var CommentAjax = new Ajax.Request(
					'comment.php?action=get_iframe_comment&url='+Url+random_str, // 请求的URL
					{
						method: 'get', 
						onComplete: showIframeComment
					}
				);
	}

	function showIframeComment(response) {
		//alert(response.responseText);
		$('comment_iframe_show').innerHTML = response.responseText;
		//alert(response.responseText);
		set_iframe_height();
	}

	function showComment(response) {
		if (response.responseText != ''){
			$('comment_show').innerHTML = response.responseText;
		}
		//alert(response.responseText);
		if (IframePub) {
			//alert("ok");
			set_iframe_height();
		}
	}

	function nextPage() {
		if (CurPage<TotalPage)
			clickPageNav(CurPage+1) ;
		else
			return false ;
	}

	function prePage() {
		if (CurPage>1)
			clickPageNav(CurPage-1) ;
		else
			return false ;
	}

	function sAlert(str,sWidth,sHeight,CloseTime){
		var msgw,msgh,bordercolor;
		msgw=sWidth;//提示窗口的宽度
		msgh=sHeight;//提示窗口的高度
		titleheight=25 //提示窗口标题高度
		bordercolor="#336699";//提示窗口的边框颜色
		titlecolor="#99CCFF";//提示窗口的标题颜色
    
		var sWidth,sHeight;
		sWidth=document.body.offsetWidth;
		sHeight=document.body.clientHeight;
		var bgObj=document.createElement("div");
		bgObj.setAttribute('id','bgDiv');
		bgObj.style.position="absolute";
		bgObj.style.top="0";
		bgObj.style.background="#777";
		bgObj.style.filter="progid:DXImageTransform.Microsoft.Alpha(style=3,opacity=25,finishOpacity=75";
		bgObj.style.opacity="0.6";
		bgObj.style.left="0";
		bgObj.style.width=sWidth + "px";
		bgObj.style.height=sHeight + "px";
		bgObj.style.zIndex = "10000";
		document.body.appendChild(bgObj);
    
		var msgObj=document.createElement("div");
		msgObj.setAttribute("id","msgDiv");
		msgObj.setAttribute("align","center");
		msgObj.style.background="white";
		msgObj.style.border="1px solid " + bordercolor;
		msgObj.style.position = "absolute";
		msgObj.style.left = "50%";
		msgObj.style.top = "30%";
		msgObj.style.font="12px/1.6em Verdana, Geneva, Arial, Helvetica, sans-serif";
		msgObj.style.marginLeft = "-200px" ;
		msgObj.style.marginTop = -75+document.documentElement.scrollTop+"px";
		msgObj.style.width = msgw + "px";
		msgObj.style.height =msgh + "px";
		msgObj.style.textAlign = "center";
		msgObj.style.lineHeight ="25px";
		msgObj.style.zIndex = "10001";
    
		var title=document.createElement("h4");
		title.setAttribute("id","msgTitle");
		title.setAttribute("align","right");
		title.style.margin="0";
		title.style.padding="3px";
		title.style.background=bordercolor;
		//title.style.filter="progid:DXImageTransform.Microsoft.Alpha(startX=20, startY=20, finishX=100, finishY=100,style=1,opacity=75,finishOpacity=100);";
		title.style.opacity="0.75";
		title.style.border="1px solid " + bordercolor;
		title.style.height="18px";
		title.style.font="12px Verdana, Geneva, Arial, Helvetica, sans-serif";
		title.style.color="white";
		title.style.cursor="pointer";
		title.innerHTML="关闭";

		//var CloseBox=document.createElement("div");
		//CloseBox.setAttribute("id","colse_box");
		//CloseBox.innerHTML="关闭";
		//CloseBox.setAttribute("align","center");
	
		//var close_box = $('close_box') ;

		title.onclick=function(){
			document.body.removeChild(bgObj);
			document.getElementById("msgDiv").removeChild(title);
			document.body.removeChild(msgObj);
			//location.reload();
			//alert("ok");
			/*
			$('quote_commentid').value = 0 ;
			$('quote_show').style.display = "none" ;
			$('content').value = '' ;

			clickPageNav(1);
			getComment(1);
			location.href = "#start" ;
			*/
			//var query_args = GetUrlParmsWithoutNewpost() ;
			//document.location.href = "comment.php?"+query_args+"&new_post=1" ;
			location.reload();
			}
		document.body.appendChild(msgObj);
		document.getElementById("msgDiv").appendChild(title);
		var txt=document.createElement("p");
		txt.style.margin="1em 0";
		txt.setAttribute("id","msgTxt");
		txt.innerHTML=str;
		document.getElementById("msgDiv").appendChild(txt);
		if (CloseTime > 0 ) {
			setTimeout(title.onclick,CloseTime) ;
			//window.onload();
		}
	}

	function showQuoteReplyForm(commentid,content) {
		
		var QuoteStr = '<tr><td style="float:left;background-color:#f5f5f5;width:350px;text-align:left;margin-left:20px;margin-bottom:10px;">引用原帖：'+content+'</td></tr>' ;
		
		var TextAreaStr = "<tr><td><textarea name='reply_content' id='reply_content' rows='7' cols='65'></textarea></td></tr>" ;
		TextAreaStr += "<tr><td><input type='button' value='发表评论' onclick='replyQuote("+commentid+");'></td></tr>" ;
		var ShowStr = "<table align='center'>"+QuoteStr + TextAreaStr+"</table>" ;
		//var ShowStr = "<table><tr><td style='float:left;background-color:#f5f5f5;width:350px;text-align:left;margin-left:20px;margin-bottom:10px;'>引用原帖："+content+"</td></tr>" ;

		sAlert(ShowStr,400,300,-1);
		//sAlert("ok",400,300);
		
	}

	function showQuoteReply(commentid) {
		var quote_content = $('comment_'+commentid).innerHTML ;
		$('quote_show').style.display = "block" ;
		//$('quote_show').innerHTML = "引用：" + delhtml(quote_content) + " [<a href='javascript://' onclick='del_quote()'>删除引用</a>]" ;
		$('quote_show').innerHTML =  "<span style='font-weight:bold;'>引用：</span><br><span style='width:100%;line-height:15px;padding:5px 5px 5px 5px;float:left;text-align:left;'>" + quote_content + "</span><span style='float:right;width:100%;text-align:right;'>[<a href='javascript://' onclick='del_quote()'>删除引用</a>]</span>" ;
		$('quote_commentid').value = commentid ;
		$('content').focus();
		if (IframePub){
			set_iframe_height() ;
		}
	}

	function showQuoteReply2(commentid) {
		var quote_content = $('comment_'+commentid).innerHTML ;
		$('quote_show').style.display = "block" ;
		//$('quote_show').innerHTML = "引用：" + delhtml(quote_content) + " [<a href='javascript://' onclick='del_quote()'>删除引用</a>]" ;
		//$('quote_show').innerHTML =  "<span style='font-weight:bold;'>引用：</span><br><span style='width:100%;line-height:15px;padding:5px 5px 5px 5px;float:left;text-align:left;'>" + quote_content + "</span><span style='float:right;width:100%;text-align:right;'>[<a href='javascript://' onclick='del_quote()'>删除引用</a>]</span>" ;
		$('quote_show').innerHTML = "<a>原帖：</a><br />"+quote_content+"<p><a href='javascript://' onclick='del_quote()'>[删除引用]</a></p>" ;
		$('quote_commentid').value = commentid ;
		$('content').focus();
		if (IframePub){
			set_iframe_height() ;
		}
	}

	function replyQuote(quoteCommentid) {
		var replyContent = $('reply_content').value ;
		var alertStr = quoteCommentid + replyContent ;
		var replyData = 'action=reply_quote&content='+replyContent+'&quote_commentid='+quoteCommentid ;
		var replyQuoteAjax = new Ajax.Request(
								'comment.php',
								{
									method: 'post',
									parameters:replyData,
									onComplete: replyResponse
								}
							);
	}

	function replyResponse(response) {
		if (response.responseText == '200') {
			var ShowStr = "<table align='center'><tr><td>您好，感谢您参与评论！<br>您的评论需要经过审核后才能发布！</td></tr></table>" ;
			sAlert(ShowStr,400,300,3000);
		}
	}

	function delhtml(text) {
        var s = 0;
        var e = 0;
        while(true)
        {
            if(text.indexOf("<") != -1)
            {
                    s =text.indexOf("<");
                    e = text.indexOf(">");
                    text = text.replace(text.substring(s,e + 1),"");
            }
            else
            {
                break;
            }
        }
        
        return text;
	}

	function del_quote() {
		$('quote_show').style.display = "none" ;
		$('quote_show').innerHTML = "" ;
		$('quote_commentid').value = 0 ;
		if (IframePub){
			set_iframe_height() ;
		}
	}

	function alertCharNums(content) {
		var MaxLength = 2000 ;
		if (content.value.length > MaxLength ){
			content.value = content.value.substring(0,MaxLength);
		}
		$('char_nums').innerHTML=content.value.length ;
	}

	function comment_pub() {
		//var channel = $('channel').value ;
		//var c_sid = $('id').value ;
		var usrname = $('usrname').value ;
		var quote_commentid = $('quote_commentid').value ;
		var content = $('content').value ;
		var password = $('password').value ;
		var vercode = $('vercode').value ;
		var anonymous_pub = $('anonymous_pub').value ;
		//var postdata = 'action=pub_new&channel='+channel+'&id='+c_sid+'&usrname='+usrname+'&quote_commentid='+quote_commentid+'&content='+content+'&password='+password+'&vercode='+vercode+'&anonymous_pub='+anonymous_pub ;
		var postdata = $H({action:'pub_new',url:Url,usrname:usrname,quote_commentid:quote_commentid,content:content,password:password,vercode:vercode,anonymous_pub:anonymous_pub}).toQueryString() ;
		//postdata = encodeURIComponent(postdata) ;
		//alert(postdata) ;
		if (content.length > 2000) {
			alert("对不起，您发表的内容超出长度限制") ;
		}
		else {
			var CommentPubAjax = new Ajax.Request(
									'comment.php',
									{
										method: 'post',
										parameters:postdata,
										onComplete: postNewResponse
									}
								);
		}
		
	}


	function postNewResponse(response) {
		
		
		if (response.responseText == "result:200") {
			$('quote_commentid').value = 0 ;
			var ShowStr = "感谢您参与迅雷评论\n您的评论需要通过审核才能发布！" ;
			NewPost = true ;
		}
		else if (response.responseText == "result:1001") {
		
			var ShowStr = "验证码错误！" ;
		}
		else if (response.responseText == "result:404") {
		
			var ShowStr = "您输入的用户名错误！" ;
		}
		else if (response.responseText == "result:500") {
		
			var ShowStr = "内部服务器错误！" ;
		}
		else if (response.responseText == "result:300") {
		
			var ShowStr = "密码错误！" ;
		}
		else if (response.responseText == "result:1002") {
		
			var ShowStr = "用户登陆失败！" ;
		}
		else if (response.responseText == "result:1003") {
		
			var ShowStr = "用户登陆失败！" ;
		}
		else if (response.responseText == "result:1004") {
		
			var ShowStr = "服务器忙，请稍后重试！" ;
		}
		else if (response.responseText == "result:1005") {
		
			var ShowStr = "您输入的评论信息为空！" ;
		}
		else {
			var ShowStr = "发表评论失败！" ;
		}
		if (response.responseText == "result:200"){
			$('content').value = '' ;
			try{
				var NoAjax = V2 ;
			}catch(e){
				var NoAjax = false ;
			}
			if (NoAjax) {
				location.href = "/comment_view.php?url="+Url ;
			}
			else {
				location.reload();
			}
		}
		else {
			alert(ShowStr) ;
		}
		
		
		//sAlert(response.responseText,300,150,2000);
		/*
		if (IframePub){
			//var query_args = GetUrlParmsWithoutNewpost() ;
			//alert(ShowStr) ;
			//var iframe_src = document.location.href ;
			//parent.document.getElementById("iframe_comment").src= iframe_src ;
			location.reload();
			//set_iframe_height() ;
		}
		else {
			//sAlert(ShowStr,300,150,2000);
			location.reload();
		}
		*/
		
	}

	function userForm() {
		//alert($('Vcode').src) ;
		if ($('user_form').style.display != "block"){
			var random_str = '&rd='+Math.floor(Math.random()*9999999)+1 ;
			$('Vcode').src = "v_code.php?"+random_str ;
			$('user_form').style.display = "block" ;
			if (IframePub){
				set_iframe_height() ;
			}
		}

	}

	function changeVcode() {
		
		var random_str = '&rd='+Math.floor(Math.random()*9999999)+1 ;
		$('Vcode').src = "v_code.php?"+random_str ;
	}

	function setAnonymous() {
		
		if ($('anonymous').checked)	{
			$('anonymous_pub').value = 1 ;
		}
		else {
			$('anonymous_pub').value = 0 ;
		}

	}

	function GetUrlParmsWithoutNewpost(){
		var args='';
		var query=location.search.substring(1);
		var pairs=query.split("&");
		var dot = '' ;
		for(var   i=0;i<pairs.length;i++)
		{
			if (i > 0 )
				dot = '&' ;
			var pos=pairs[i].indexOf('=');
            if(pos==-1)   continue;
            var argname=pairs[i].substring(0,pos);//提取name
            var value=pairs[i].substring(pos+1);//提取value
			if (argname != 'new_post') {
				args += dot + argname + '=' + value ;
			}
            args[argname]=unescape(value);//存为属性   
		}
		return args;
	}

	function goRegister() {
		window.open("http://i.xunlei.com/register/register.htm", "_blank"); 
	}
	function deleteCookie(name){
		document.cookie   =   name+"=;domain=xunlei.com;expires="+(new   Date(0)).toGMTString();
	}
	function goLogout() {
		deleteCookie('sessionid') ;
		deleteCookie('is_comment_login') ;
		location.reload();
	}
	function set_iframe_height(){
		//alert("height:ok");
		var iframe_height = document.body.scrollHeight+"px" ;
		//alert(iframe_height) ;
		parent.document.getElementById("iframe_comment").height=iframe_height;
		//window.setTimeout(publishHeight, 300);
	}

	function comment_check() {

		var content = $('content').value ;
		var usrname = $('usrname').value ;
		var password = $('password').value ;
		var vercode = $('vercode').value ;
		var anonymous_pub = $('anonymous_pub').value ;
		var islogin = $('islogin').value ;
		//alert(anonymous_pub) ;
		if (content == '') {
			alert("您输入的评论内容为空!") ;
			$('content').focus();
			return false ;
		}

		if (content.length < 5){
			alert("您输入的字数太少!") ;
			$('content').focus();
			return false ;
		}
		if (anonymous_pub == '0'  && islogin == '0' && (usrname == '' || password == '' )) {
			if (confirm("是否选择匿名发表？如果不是，请填写用户信息")) {
				$('anonymous_pub').value = 1 ;
				$('anonymous').checked = true ;
				$('usrname_form').style.display = "none" ;
				$('passport_form').style.display = "none" ;
				$('content').focus();
				$('usrname').value = '' ;
				$('password').value = '' ;
				//return false ;
			}
			else {
				if ($('usrname').value == '') {
					$('usrname').focus() ;
				}
				else {
					$('password').focus() ;
				}
				return false ;
			}
		}
		if (vercode.length == 0) {
			alert("请填写验证码!") ;
			$('vercode').focus() ;
			return false ;
		}
		if (vercode.length != 4) {
			alert("验证码长度不对!") ;
			$('vercode').focus() ;
			return false ;
		}
		if (anonymous_pub == '1' ) {
			$('usrname').value = '' ;
			$('password').value = '' ;
		}
		comment_pub();
	}

	function doAction(commentid,action) {
		var random_str = '&rd='+Math.floor(Math.random()*9999999)+1 ;
		var SupportAjax = new Ajax.Request(
					'comment.php?action=do_action&action_type='+action+'&commentid='+commentid + '&url=' + Url + random_str, // 请求的URL
					{
						method: 'get', 
						onComplete: resetActionNums
					}
				);
	}
	function resetActionNums(response) {
		//alert(response.responseText);
		if (response.responseText != "") {
			var ReturnStr = response.responseText ;
			var tmp = ReturnStr.split("_") ;
			$(tmp[1]+'Nums_'+tmp[0]).innerHTML = "(" + tmp[2] + ")" ;
		}
		else {
			alert("您已经对该评论投票过了！") ;
		}
		
	}